package mi.payment.transaction;

import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sunmi.pay.hardware.aidl.AidlConstants;

import mi.payment.BuildConfig;
import mi.payment.MyApplication;
import mi.payment.R;
import mi.payment.constants.Params;
import mi.payment.settings.SalesSlipPrintSettings;
import mi.payment.settings.SalesSlipPrintSettingsOperation;
import mi.payment.settings.SignOperation;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.SystemDateTime;
import mi.payment.utils.UIUtils;
import mi.payment.utils.transaction.ParamsInitTask;

/**
 * 有菜单界面的基类Activity
 */
public abstract class BaseMenuActivity extends ParentActivity implements ParamsCheckExt.LogonStateListener {

    private View mHeadLine;
    private LinearLayout mItemsLayout;
    private DrawerLayout mDrawerLayout;

    public MenuController mController;
    public ParamsCheckExt mParamsCheckExt;

    public abstract View getView();

    @Override
    public void onLogonFailed() {
        closeMenu();
    }

    @Override
    public void onLogonSuccess() {
        closeMenu();
        mController.refreshCurrentOperator();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_menu);

        initView();

        setKeepScreenOn(false);

        if (Params.userInfo == null) {
            Params.userInfo = mUserInfoModel.queryUserInfoById("01");
        }

        // 获取最后签到的时间
        int lastSignTime = SignOperation.getInstance().getLastSignTime();
        int currentYYYYMMDD = SystemDateTime.getCurrentYYYYMMDD();
        if (lastSignTime == currentYYYYMMDD) {
            ParamsInitTask.isChecked = true;
        }
    }

    private void initView() {
        MenuScrollview scrollview = findViewById(R.id.scroll_view);
        FrameLayout containerView = findViewById(R.id.container);
        mItemsLayout = findViewById(R.id.item_layout);
        mDrawerLayout = findViewById(R.id.drawer_layout);

        initHeadView();

        View container = getView();
        containerView.addView(container);

        mParamsCheckExt = new ParamsCheckExt(this);
        mParamsCheckExt.setOnLogonStateListener(this);

        mController = new MenuController(this, mParamsCheckExt);

        scrollview.setOnScrollBottom(
                new MenuScrollview.ScrollBottomListener() {

                    @Override
                    public void onBottom() {
                        mHeadLine.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onTop() {
                        mHeadLine.setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void down() {
                        mHeadLine.setVisibility(View.VISIBLE);
                    }

                }
        );

        // 如果是L3调用的话，右侧菜单不可以滑动
        if (Params.sL3Params != null) {
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        }
    }

    private void initHeadView() {
        SalesSlipPrintSettings salesSlipPrintSettings = SalesSlipPrintSettingsOperation.getInstance().getSalesSlipPrintSettings();
        // 标题头部
        LinearLayout headLayout = findViewById(R.id.head_layout);
        View view = View.inflate(this, R.layout.main_item_menu_head, null);

        mHeadLine = view.findViewById(R.id.view_line);
        mHeadLine.setVisibility(View.INVISIBLE);

        // 标题头部文字显示
        boolean printChannelInfo = salesSlipPrintSettings.isPrintChannelInfo();
        TextView tvAppName = view.findViewById(R.id.tv_name);
        if (printChannelInfo) {
            tvAppName.setText(R.string.app_name);
        } else {
            tvAppName.setText("收银台");
        }

        // 版本信息
        TextView tvVersion = view.findViewById(R.id.tv_version_name);
        String version = getString(R.string.version_name) + BuildConfig.VERSION_NAME;
        tvVersion.setText(version);

        // 客服电话
        String hotLine = salesSlipPrintSettings.getHotLine();
        TextView tvTel = view.findViewById(R.id.tv_tel);
        if (hotLine != null && hotLine.length() > 0) {
            hotLine = getString(R.string.index_telephone_number) + hotLine;
            tvTel.setText(hotLine);
            tvTel.setVisibility(View.VISIBLE);
        } else {
            tvTel.setVisibility(View.GONE);
        }

        headLayout.addView(view);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Params.sL3Params == null) {
            UIUtils.screenMonopoly(-1);
        }

        mController.init(mItemsLayout);

        try {
            // 卡片下电
            int icValue = AidlConstants.CardType.IC.getValue();
            int nfcValue = AidlConstants.CardType.NFC.getValue();
            MyApplication.app.readCardOpt.cardOff(icValue);
            MyApplication.app.readCardOpt.cardOff(nfcValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        closeMenu();
    }

    @Override
    public void onBackPressed() {
        boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(Gravity.START);
        if (isDrawerOpen) {
            closeMenu();
        } else {
            exitActivity();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void openMenu() {
        boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(Gravity.START);
        if (isDrawerOpen) return;
        mDrawerLayout.openDrawer(GravityCompat.START);
    }

    public void closeMenu() {
        boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(Gravity.START);
        if (isDrawerOpen) {
            mDrawerLayout.closeDrawer(Gravity.START);
        }
    }

    private long clickTime = 0;

    private void exitActivity() {
        long time = System.currentTimeMillis() - clickTime;
        if (time > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            clickTime = System.currentTimeMillis();
        } else {
            sendCloseAppBroadcast();
        }
    }


}
